{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Trapping Rainwater\n",
    "\n",
    "[The Original Question](https://mp.weixin.qq.com/s/UUc1dn4BqjgHMfUx8bk3yA)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question\n",
    "\n",
    "You have a landscape, in which puddles can form. You are given an array of non-negative integers representing the elevation at each location. Return the amount of water that would accumulate if it rains."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example\n",
    "\n",
    "`[0,1,0,2,1,0,1,3,2,1,2,1]` should return `6` because 6 units of water can get trapped here.\n",
    "\n",
    "The visible graph is shown below. In the graph, `_` represents 0 and it can't be filled by rain, `X` represents the block and `O` represents the space that can be filled.\n",
    "\n",
    "```text\n",
    "                      X\n",
    "          X  O  O  O  X  X  O  X\n",
    " _  X  O  X  X  O  X  X  X  X  X  X\n",
    "[0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def capacity(array):\n",
    "    \n",
    "    # In this case, rain could not be saved.\n",
    "    if array is None or len(array) <= 2:\n",
    "        return 0\n",
    "    \n",
    "    # The maximum height of 2 borders.\n",
    "    max_left = array[0]\n",
    "    max_right = array[-1]\n",
    "    \n",
    "    # The capacity\n",
    "    result = 0\n",
    "    \n",
    "    # The index of current border\n",
    "    left = 1\n",
    "    right = len(array) - 2\n",
    "    \n",
    "    # Accroding to definition,\n",
    "    # left border couldn't exists in the right side of the right border.\n",
    "    while left <= right:\n",
    "        \n",
    "        # The capacity of a tank relates to the minimum of 2 borders.\n",
    "        if max_left <= max_right:\n",
    "            \n",
    "            # If current block is lower than both borders,\n",
    "            # rain could be saved here.\n",
    "            # Otherwise, it can't.\n",
    "            result += max(0, max_left - array[left])\n",
    "            \n",
    "            # If current point is higher than left border.\n",
    "            if array[left] > max_left:\n",
    "                \n",
    "                # Update it!\n",
    "                max_left = array[left]\n",
    "                \n",
    "            # Move to the next point\n",
    "            left += 1\n",
    "            \n",
    "        # The following code has the same intensions.\n",
    "        else:\n",
    "            result += max(0, max_right - array[right])\n",
    "            if array[right] > max_right:\n",
    "                max_right = array[right]\n",
    "                continue\n",
    "            right -= 1\n",
    "            \n",
    "    # Return the results\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "print(capacity([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
